package com.wzp.yyptproject.service;

import com.wzp.yyptproject.entity.*;
import com.wzp.yyptproject.repository.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class DoctorServiceimp implements DoctorService {
    @Autowired
    private DoctorRepository doctorRepository;
    @Autowired
    private KeShiRepository keShiRepository;
    @Autowired
    private JiBingRepository jiBingRepository;
    @Autowired
    private ZiKeShiRepository ziKeShiRepository;
    @Autowired
    private FkBingRepository fkBingRepository;
    @Autowired
    private NkBingRepository nkBingRepository;
    @Autowired
    private WkBingRepository wkBingRepository;
    @Autowired
    private EkBingRepository ekBingRepository;
    @Autowired
    private DoctorService doctorService;

    @Override
    public Doctor findAndByDDid(Integer did) {
        return doctorRepository.findAndByDDid(did);
    }

    @Override
    public Doctor findAndByDDname(String ddname) {
        return doctorRepository.findAndByDDname(ddname);
    }

    @Override
    public List<Doctor> findAll() {
        return doctorRepository.findAll();
    }

    public List<Doctor2> DoctorMess(List<Doctor> doctorList) {
        List<Doctor2> doctor2List=new ArrayList<>();
        for (Doctor doctor:doctorList) {
            Doctor2 doctor2 = new Doctor2();
            doctor2.setDDid(doctor.getDDid());
            doctor2.setDDName(doctor.getDDname());
            doctor2.setDAge(doctor.getDAge());
            doctor2.setDSex(doctor.getDSex());
            doctor2.setDAddress(doctor.getDAddress());
            doctor2.setDJobnum(doctor.getDJobnum());
            doctor2.setDpassword(doctor.getDPassword());
            doctor2.setBBid(doctor.getBBid());
            doctor2.setKKid(doctor.getKKid());

            KeShi keShi = keShiRepository.findAllByKid(doctor.getKKid());
            ZiKeShi ziKeShi = ziKeShiRepository.findAndByZkid(doctor.getZKid());
            System.out.println(doctor.getZKid());
            System.out.println(ziKeShi.getZkname());
            doctor2.setDKname(keShi.getKname());
            doctor2.setZKname(ziKeShi.getZkname());
            System.out.println(doctor2.getZKname());
            JiBing jiBing = new JiBing();
            Optional<JiBing> optional = jiBingRepository.findById(doctor.getBBid());
            if (optional.isPresent()) {
                jiBing = optional.get();
            }
            System.out.println(jiBing.toString());
            if (jiBing.getNbid()!=null){
                NkBing nkBing = nkBingRepository.findAndByNbid(jiBing.getNbid());
                doctor2.setNbgoodnum(nkBing.getNbgoodnum());
            }else {
                if (jiBing.getWbid()!=null){
                    WkBing wkBing = wkBingRepository.findAndByWbid(jiBing.getWbid());
                    doctor2.setWbgoodnum(wkBing.getWbgoodnum());
                }else {
                    if (jiBing.getFbid()!=null){
                        FkBing fkBing = fkBingRepository.findAndByFbid(jiBing.getFbid());
                        doctor2.setFbgoodnum(fkBing.getFbgoodnum());
                        System.out.println(fkBing.getFbgoodnum());
                    }else {
                        if (jiBing.getEbid()!=null){
                            EkBing ekBing = ekBingRepository.findAndByEbid(jiBing.getEbid());
                            doctor2.setEbgoodnum(ekBing.getEbgoodnum());
                        }
                    }
                }
            }




            doctor2.setBBname(jiBing.getBname());
            doctor2.setDHead(doctor.getDHead());
            doctor2.setDPhonenum(doctor.getDPhonenum());
            doctor2.setDTitle(doctor.getDTitle());
            doctor2.setDStatus(doctor.getDStatus());
            doctor2List.add(doctor2);
        }
        return doctor2List;
    }

    public Page<Doctor2> findByPage(int pageCode, int pageSize) {

       List <Doctor2> doctor2List= doctorService.DoctorMess(doctorRepository.findAll());

        List<Doctor2> list1=new ArrayList<>();
        for(Doctor2 doctor2 :doctor2List){
            Pageable pageable = new PageRequest(pageCode, pageSize);
            Page<Doctor> doctors=doctorRepository.findAll(pageable);
            System.out.println("理科"+doctor2.getKKid());
            List<Doctor2> list=DoctorMess(doctors.getContent());
            System.out.println("333**"+list.size());
            System.out.println(""+list);
            if (doctor2.getKKid()==3){
                System.out.println("理科"+doctor2.getKKid());
                Doctor2 doctor21=new Doctor2();
                doctor21.setNbgoodnum(doctor2.getNbgoodnum());
                doctor21.setDTitle(doctor2.getDTitle());
                doctor21.setZKname(doctor2.getZKname());
                doctor21.setDDid(doctor2.getDDid());
                doctor21.setDAddress(doctor2.getDAddress());
                doctor21.setDKname(doctor2.getDKname());
                doctor21.setBBname(doctor2.getBBname());
                doctor21.setDJobnum(doctor2.getDJobnum());
                doctor21.setDAge(doctor2.getDAge());
                doctor21.setKKid(doctor2.getKKid());
                doctor21.setDHead(doctor2.getDHead());
                doctor21.setWbgoodnum(doctor2.getWbgoodnum());
                doctor21.setEbgoodnum(doctor2.getEbgoodnum());
                doctor21.setKKid(doctor2.getKKid());
                doctor21.setDPhonenum(doctor2.getDPhonenum());
                doctor21.setFbgoodnum(doctor2.getFbgoodnum());
                doctor21.setNbgoodnum(doctor2.getNbgoodnum());
                doctor21.setDSex(doctor2.getDSex());
                doctor21.setDStatus(doctor2.getDStatus());
                doctor21.setDDName(doctor2.getDDName());
                list1.add(doctor21);
            }
            System.out.println("内科数量"+list1.size());
        }
        return new PageImpl<>(list1);
    }
    @Override
    public Integer findFuKeCount() {
      Integer doctor = doctorRepository.findAndByKKid(3).size();
        return doctor;
    }
}
